DEV_DIR=./dev
BUILD_DIR=./build
SRC_DIR=.

default:
	@echo targets: clean build 

clean: dist-clean
	rm -rf $(BUILD_DIR)

dist-clean: doctrine-clean

doctrine-clean:
	rm -rf dbmodels dbsql

build: prepare-build doctrine-build prepare-dist

update-i18n:
	echo '[encoding: UTF-8]' > app/data/i18n/POTFILES.in
	find . -name '*.php'|grep -v '/build/' | grep templates | sed -e 's/^\./.\/..\/../'  >> app/data/i18n/POTFILES.in
	cd app/data/i18n/ && intltool-update --pot --gettext-package=messages --verbose
	msgmerge -U app/data/i18n/ru.po app/data/i18n/messages.pot

compile-i18n: update-i18n
	msgmerge -o - app/data/i18n/ru.po app/data/i18n/messages.pot | msgfmt -c -o app/data/i18n/ru.mo -

prepare-build:
#	copy all required files	
	mkdir -p $(BUILD_DIR)/app
	cp -r $(SRC_DIR)/app/config $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/app/config.php $(BUILD_DIR)/app/config.php-dist
	cp -r $(SRC_DIR)/app/modules $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/app/models $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/app/lib $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/app/data $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/app/templates $(BUILD_DIR)/app/
	cp -r $(SRC_DIR)/pub $(BUILD_DIR)/
	cp -r $(SRC_DIR)/utils $(BUILD_DIR)/
	
#	delete subversion service dirs
	find $(BUILD_DIR) -name .svn -type d -print0|xargs -0 rm -rf

doctrine-build: doctrine-clean
	mkdir -p dbmodels
	mkdir -p dbsql
	./utils/doctrine generate-models-yaml
	./utils/doctrine generate-sql
	mkdir $(BUILD_DIR)/init
	cp dbsql/schema.sql dev/data/init-mysql.sql $(BUILD_DIR)/init/

# copy all required files to their directories
	cp -r dbmodels/* $(BUILD_DIR)/app/lib/doctrine/

prepare-dist:
	cd build && \
	tar cjf ../blog.tar.bz2 .
